package com.petmall.controller;

import com.petmall.entity.User_md5;
import com.petmall.service.UserService;
import com.petmall.util.HttpStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by pig2guang on 2017/4/18.
 * 该Controller以及相应调用方法并不属于本系统的功能，只是用来做些测试临时用的
 * Restful , MD5
 */
@RestController
@Api(value = "restful服务")
public class HelloController {

    @Autowired
    private UserService userservice;

    @GetMapping(value = "/hello")
    @ApiOperation("欢迎界面")
    public String sayHello(){
        return "Welcome to MC Petmall !";
    }

    @RequestMapping(value = "/addMd5User", method = RequestMethod.POST)
    @ApiOperation("用户注册")
    public Map<String,Object> addMd5User(@RequestBody User_md5 user) {
        Boolean isExist_user = userservice.isExistMd5User(user);
        Map<String, Object> result = new HashMap<String, Object>();
        if(isExist_user == true){
            result.put("code", HttpStatus.NOT_PERMIT);
            result.put("user", null);
            return result;
        }else {
            userservice.addMd5User(user);
            result.put("code", HttpStatus.CREATED);
            result.put("user", user);
            return result;
        }
    }

    @RequestMapping(value = "/loginCheckMd5User", method = RequestMethod.POST)
    @ApiOperation("用户登录")
    public String loginCheckMd5User(@RequestBody User_md5 user) {
        Boolean is_user = userservice.findMd5User(user);
        if(is_user == true)
            return HttpStatus.SUCCESS;
        else
            return HttpStatus.NOT_FOUND;
    }

}
